﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SS.Server.AppSession;
using SS.Server.ReceiveFilter;
using SuperSocket.SocketBase.Protocol;
using Mode;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace SS.Server
{
    internal class ThreadWithParam
    {
        //要用到的属性，也就是我们要传递的参数
        private DemoSession _session;
        private BinaryRequestInfo _requestInfo;
        //      private MainForm _main;

        //包含参数的构造函数
        public ThreadWithParam(DemoSession seesion, BinaryRequestInfo requestInfo)//, MainForm main)
        {
            _session = seesion;
            _requestInfo = requestInfo;
            // _main = main;

        }

        /// <summary>
        /// 要丢给线程执行的方法
        /// </summary>
        public void ThreadProc()
        {
            try
            {
                if (_requestInfo == null)
                {
                    return;
                }
                MessageByte MsByte = SerializationHelpers.ByteToObject(_requestInfo.Body, 0, _requestInfo.Body.Length) as MessageByte;

                int InfoType = MsByte.infoType;
                switch (InfoType)
                {
                    case InformationTypes.AddEmployee:   //连接 //infoType.Add
                        #region 1、测试接收客户端的数据后显示出来
                        /*
                        MessageStr msr = SerializationHelpers.ByteToObject(MsByte.MsgByte,0,MsByte.MsgByte.Length)as MessageStr;
                        Console.WriteLine("AddEmployee");
                        Console.WriteLine("名称：{0}",msr.Name);
                        Console.WriteLine("Msg：{0}", msr.Msg);
                        Console.WriteLine("当前客户端个数：" + _session.AppServer.SessionCount);

                        byte[] info = SerializationHelpers.ObjectToByte(msr);
                        MessageByte SendMsgByte = new MessageByte { infoType = InformationTypes.AddEmployee, MsgByte = info };
                        byte[] Sendinfo = SerializationHelpers.ObjectToByte(SendMsgByte);
                        var o = new ArraySegment<byte>(BitConverter.GetBytes(Sendinfo.Length).Concat(Sendinfo).ToArray());
                        _session.Send(o);
                        */
                        #endregion

                        #region 2、测试读取数据库的DataSet
                        
                          string tmpStr = "Data Source=" + "SINOMAC" + "\\SQLEXPRESS;Initial Catalog=PrintPMIS;User Id=sa;Password=ssa;";
                          DataSet ds = new DataSet();
                          try
                          {
                              //读取数据库
                              SqlConnection con = new SqlConnection(tmpStr);
                              SqlDataAdapter dap = new SqlDataAdapter("PRO_Porject_AllView", con);
                              dap.SelectCommand.CommandType = CommandType.StoredProcedure;
                              dap.Fill(ds);
                              con.Close();

                              byte[] info = SerializationHelpers.ObjectToByte(ds);

                              Console.WriteLine(info.Length);
                              Console.WriteLine("AddEmployee");

                              MessageByte SendMsgByte = new MessageByte { infoType = InformationTypes.AddEmployee, MsgByte = info };
                              byte[] Sendinfo = SerializationHelpers.ObjectToByte(SendMsgByte);
                              var o = new ArraySegment<byte>(BitConverter.GetBytes(Sendinfo.Length).Concat(Sendinfo).ToArray());
                              _session.Send(o);
                          }

                          catch (Exception E)
                          {
                              Console.WriteLine(E.Message.ToString());
                          }

                          //显示当前有多少个客户端在线
                          Console.WriteLine("当前客户端个数：" +_session.AppServer.SessionCount);
                       
                        #endregion

                        //   OnConnection();
                        break;
                    case InformationTypes.DeleteEmployee:    //注册
                        // Login();
                        break;
                    case 2:    //读写数据库
                               //  OpreateDbServer();
                        break;
                    case 3:  // 接收文件
                             // GetFiles();
                        break;
                    case 4:  //转发消息
                             // SendMessageToClient();
                        break;
                    case 5:  //获取在线用户
                             //  GetOnLineUser();
                        break;
                    default:
                        break;
                }
            }
            catch// (Exception errException)
            {

            }

        }

        /// <summary>
        /// 用户连接成功返回 可用于心跳检测
        /// </summary>
        private void OnConnection()
        {
            //    string connet = JsonConvert.SerializeObject(new MessageBase());
            //    _session.Send(connet);
        }

    }
}
